<?php


class PTHelper
{
	protected $site;
	protected $db;
	protected $page;
	
	public $user_id;
	public $ok_data;
	public $apiconn;
	public $server;
	public $serv_data;
	public $app_key;
	public $session_key;
	
	
	public function __construct( Site &$site )
	{
		$this->site = &$site;
		$this->db	= &$site->db;
		$this->page	= &$site->page;
		$this->user_id = -1;
		$this->serv_data = array();
		$this->ok_data = array();
	}
	
	
	public function db_auth_user()
	{
		$auth = intval($this->site->_get("authorized"));
		if ( $auth )
		{
			$this->ok_data = $this->site->get;
			$this->user_id = $this->site->_get("logged_user_id");
			$this->apiconn = $this->site->_get("apiconnection");
			$this->server = $this->site->_get("api_server");
			$this->app_key = $this->site->_get("application_key");
			$this->session_key = $this->site->_get("session_key");
			
			if ( $this->db->select("*", "gamers")->where("`socid`='".$this->user_id."'")->result()->has_rows() )
			{
				$this->serv_data = $this->db->fetch_assoc();
				$this->db->update("gamers")->q(" SET `last_update`='".time()."'")->where("`id`='".$this->user_id['id']."'")->result();
			}
			else
			{
				$params = array(
					'fields=gender,name,pic_1',
					'uids='.$this->user_id
				);
				$obj = $this->call_api("users.getInfo", $params);
				$obj = $obj[0];
				
				if ( $obj )
				{
					$image = file_get_contents($obj['pic_1']);
					file_put_contents("images/ava_".$this->user_id.".jpg",$image);
					
					$this->db->insert("gamers")->values(array('name','socid','image','last_update'), array($obj['name'], $obj['uid'], "images/ava_".$this->user_id.".jpg", time()))->result();
					if ( $this->db->fetch_bool() )
					{
						if ( $this->db->select("*", "gamers")->where("`socid`='".$this->user_id."'")->result()->has_rows() )
						{
							$this->serv_data = $this->db->fetch_assoc();
						}
					}
				}
				else
				{
					header("Location: /");
				}
			}
		}
	}
	
	
	public function header_html()
	{
		header("Content-type: text/html; charset=UTF-8");
	}
	
	
	public function header_json()
	{
		header("Content-type: application/json; charset=UTF-8");
	}
	
	
	public function load_header_form()
	{
		$this->page->loadTplToVar("headerForm", "home/header.tpl");
		$this->page->add_css("base.css");
		$this->page->add_js("jquery-2.0.0.js");
		$this->page->setVar("player_avatar", "/".$_SESSION['image']);
		$this->page->setVar("player_name", $_SESSION['name']);
		
		$lev = explode(".", $_SESSION['level'], 2);
		
		$lev_base = $lev[0];
		$lev_proc = isset($lev[1]) ? $lev[1] : 0;
		
		if ( strlen($lev_proc) < 2 )
		{
			$lev_proc .= "0";
		}
		$energy_max = floor(floatval($_SESSION['level']) * 60.0);
		$_SESSION['energy_max'] = $energy_max;
		
		$this->page->setVar("player_level", $lev_base);
		$this->page->setVar("player_level_procent", $lev_proc);
		
		$this->page->setVar("player_money", $_SESSION['money']);
		$this->page->setVar("player_energy", $_SESSION['curr_energy']);
		$this->page->setVar("player_energy_max", $energy_max);
		
	}
	
	public function call_api( $method, $params )
	{
		$meth = str_replace(".", "/", $method);
		
		$def = array(
			"application_key=".$this->app_key,
			'format=JSON'
		);
		
		$params = array_merge($params, $def);
		$obj = array();
		sort($params);
		
		$sig = md5( join("", $params) . MYOK_SECRET_KEY );
		$req = $this->server . "api/$meth?sig=$sig&".join("&",$params);
		
		$page = file_get_contents( $req );
		$obj = json_decode($page, true);
		return $obj;
	}
	
	public function last_update()
	{
		$this->db->update("gamers")->q(" SET `last_update`='".time()."'")->where("`id`='".$_SESSION['id']."'")->result();
	}
}



